-- BreadMill
-- Created by cheny3 Sep/12/2016
-- 面包磨坊

-- 初始化
function initBreadMill(self)
    self.item:removeAllChildren();

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            local function recoverBishop()
                if not ME.user.bishop then
                    alert(getLocStr("has_no_bishop"));
                    return;
                end

                local ret = BishopM.canCure(self.gridData:getPos());
                if true == ret then
                    DungeonActionM.go("bishop_recover", self.gridData:getPos());
                    alert(buildingInfo["bonus_tip"]);
                elseif "cost_lack" == ret then
                    alert(getLocStr("search_point_not_nough"));
                elseif "bishop_dead" == ret then
                    alert(getLocStr("bishop_dead"));
                end
            end

            -- 打开建筑界面
            self:openBuildingView(recoverBishop);

            -- 音效
            AudioM.playFx("workshop_store");
        end
    end

    -- 建筑图标
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;
end
